﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for DanhGiaCtr
/// </summary>
public class DanhGiaCtr
{
	public DanhGiaCtr()
	{

	}

    public EDanhGia LayDanhGia(string MaDG)
    {

        try
        {
            DataTable tableDanhGia = DataProvider.GetData("Select * from DanhGia where MaDG=@MaDG and Active=1 order by Time DESC","@MaDG",MaDG);
            EDanhGia danhGia = new EDanhGia();
            danhGia.MaSP = tableDanhGia.Rows[0]["MaSP"].ToString();
            danhGia.NoiDung = tableDanhGia.Rows[0]["NoiDung"].ToString();
            danhGia.Nguoigoi = tableDanhGia.Rows[0]["Nguoigui"].ToString();
            danhGia.TieuDe = tableDanhGia.Rows[0]["TieuDe"].ToString();
            danhGia.ThoiGian= DateTime.Parse(tableDanhGia.Rows[0]["Time"].ToString());
            danhGia.Diem = Int32.Parse(tableDanhGia.Rows[0]["Diem"].ToString());
            return danhGia;
        }
        catch (Exception ex)
        {
            throw ex;
        }
       
    }

    public DataTable LayDanhGia()
    {

        try
        {
            string sql = "select * from DanhGia where Active=1";
            return DataProvider.GetData(sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public DataTable LayDiemDanhGia(string maSP)
    {

        try
        {
            string sql = "select round(avg(Diem),0) as Diem from DanhGia where masp=@MaSP and trangthai=1 group by MaSP";
            return DataProvider.GetData(sql, "@MaSP",maSP);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    
    
    public bool ThemDanhGiaE(EDanhGia DanhGia)
    {
        try
        {
            DataProvider.ExecuteNonQuery("INSERT INTO DanhGia VAlUES(@MaSP,@TieuDe,@NoiDung,@NguoiGoi,@Time,@Diem)",
              "@MaSP", DanhGia.MaSP, "@TieuDe", DanhGia.TieuDe, "@NoiDung", DanhGia.NoiDung, "@NguoiGoi", DanhGia.Nguoigoi,"@Time",DanhGia.ThoiGian,"@Diem",DanhGia.Diem);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public bool RemoveDanhGia(int MaDG)
    {
        try
        {
            DataProvider.ExecuteNonQuery("DELETE FROM DanhGia WHERE MaDG=@MaDG", "@MaDG", MaDG);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;

        }
    }

    public bool DeleteDanhGia(int MaDG)
    {
        try
        {
            DataProvider.ExecuteNonQuery("update DanhGia set Active=0 WHERE MaDG=@MaDG", "@MaDG", MaDG);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;

        }
    }

    public DataTable LayMaDGMax()
    {
        string sql = "select  Max(MaDG) from DanhGia ";
        return DataProvider.GetData(sql);
    }

    //public void ThemDanhGia(EDanhGia danhGia) ????????? :-(
    public bool ThemDanhGia(EDanhGia danhGia)
    {
        string sqlInsert = "insert into DanhGia(masp,tieude,noidung,nguoigoi,thoigian,diem,trangthai) values(@masp,@tieude,@noidung,@nguoidung,@time,@diem,@trangthai)";
        DataProvider.ExecuteNonQuery(sqlInsert,"@masp",danhGia.MaSP, "@tieude",danhGia.TieuDe, "@noidung", danhGia.NoiDung, "@nguoidung", danhGia.Nguoigoi, "@time", danhGia.ThoiGian,"@diem",danhGia.Diem,"@trangthai",danhGia.TrangThai);
        return true;
    }

    public DataTable LaySPDanhGia(string masp)
    {
        string sql = "Select * from DanhGia where MaSP=@a and trangthai='true' order by Thoigian DESC";
        return DataProvider.GetData(sql, "@a", masp);
    }
    //Hanh
    public DataTable LayDanhGia1()
    {

        try
        {
            string sql = "select d.*,s.TenSP from DanhGia d,SanPham s where d.TrangThai=1 and d.masp=s.masp";
            return DataProvider.GetData(sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool DeleteDanhGiaH(int MaDG)
    {
        try
        {
            DataProvider.ExecuteNonQuery("update DanhGia set TrangThai=0 WHERE MaDG=@MaDG", "@MaDG", MaDG);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;

        }
    }
    public DataTable timKiemDanhGia(string timKiem)
    {
        try
        {
            string sql = "select d.*,s.TenSP from DanhGia d,SanPham s where d.TrangThai=1 and d.masp=s.masp and ((d.Noidung like @timKiem) or (d.TieuDe like @timKiem))";
            return DataProvider.GetData(sql, "@timKiem", '%' + timKiem + '%');
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}